package com.msmall.community.bbs.model;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.msmall.base.model.BaseObject;
import com.msmall.base.util.Constants;
import com.msmall.base.util.StringUtil;
import com.msmall.community.ApplicationManager;
import com.msmall.community.bbs.service.TopicManager;
import com.msmall.community.bbs.util.BbsUtil;


@Entity
@Table(name="bbs_topic")
public class Topic extends BaseObject implements Serializable{

	private static final long serialVersionUID = 7512441383891633650L;

	@Id
	@Column(name="topic_id")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Long topicId;
	
	@Column(name="keyword_id")
	private Long keywordid;
	
	@Column(name="nickname")
	private String nickname;
	
	@Column(name="signature")
	private String signature;

	@Column(name="title")
	private String title;
	
	@Column(name="file_path")
	private String filePath; // 文件路径

	@Column(name="file_name")
	private String fileName; //文件名称
	
	@Column(name="file_ext")
	private String fileExt; // 文件扩展名
	
	@Column(name="file_flag")
	private String fileFlag; // 文件标识，目前生产了什么文件
	
	/**
	 * 内容摘要 300长度 删除了所有格式
	 */
	@Column(name="content_abstract",length=500)
	private String contentAbstract;
	@Lob
	@Basic(fetch=FetchType.LAZY)
	@Column(name="content")
	private String content;

	@Column(name="class_rule_id")
	private Long classRuleId;

	@Column(name="click_amount")
	private Integer clickAmount;

	@Column(name="thread_amount")
	private Integer threadAmount;

	
	@Column(name="create_time")
	@Temporal(TemporalType.TIMESTAMP)
	private Date createTime;

	/**
	 * 作者的最后修改日期
	 */
	@Column(name="last_modify_time")
	@Temporal(TemporalType.TIMESTAMP)
	private Date lastModifyTime;
	
	/**
	 * 最后回复日期
	 */
	@Column(name="last_thread_time")
	@Temporal(TemporalType.TIMESTAMP)
	private Date lastThreadTime;
	
	@Column(name="enabled")
	private Short enabled;

	@Column(name="top")
	private Short top;

	@Column(name="commend")
	private Short commend;
	
	@Column(name="elite")
	private Short elite;

	/**
	 * WWW 1 , WAP 2
	 */
	@Column(name="source")
	private Short source;

	/**
	 * 转载 1， 原创2
	 * Constantdb.FROM_TRANSFER
	 */
	@Column(name="original")
	private Short original;

	@Column(name="reply")
	private Short reply;

	@Column(name="notice")
	private Short notice;

	@Column(name="user_id")
	private Long userid;
	
	@Column(name="board_id")
	private Long boardid;

	public Topic() {
		super();
	}

	public Long getKeywordid() {
		return keywordid;
	}

	public void setKeywordid(Long keywordid) {
		this.keywordid = keywordid;
	}

	public String getContent50(){
		return StringUtil.getChineseSubString(contentAbstract, 50, "");
	}
	public String getContent100(){
		return StringUtil.getChineseSubString(contentAbstract, 100, "");
	}
	public String getContent150(){
		return StringUtil.getChineseSubString(contentAbstract, 150, "");
	}
	public String getContent200(){
		return StringUtil.getChineseSubString(contentAbstract, 200, "");
	}
	
	public Long getTopicId() {
		return this.topicId;
	}

	public void setTopicId(Long topicId) {
		this.topicId = topicId;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getSignature() {
		return this.signature;
	}

	public void setSignature(String signature) {
		this.signature = signature;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Long getClassRuleId() {
		return this.classRuleId;
	}

	public void setClassRuleId(Long classRuleId) {
		this.classRuleId = classRuleId;
	}

	public Integer getClickAmount() {
		return this.clickAmount;
	}

	public void setClickAmount(Integer clickAmount) {
		this.clickAmount = clickAmount;
	}

	public Integer getThreadAmount() {
		return this.threadAmount;
	}

	public void setThreadAmount(Integer threadAmount) {
		this.threadAmount = threadAmount;
	}

	public Date getCreateTime() {
		return this.createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Long getUserid() {
		return userid;
	}

	public void setUserid(Long userid) {
		this.userid = userid;
	}

	public Date getLastModifyTime() {
		return lastModifyTime;
	}

	public void setLastModifyTime(Date lastModifyTime) {
		this.lastModifyTime = lastModifyTime;
	}

	public Date getLastThreadTime() {
		return lastThreadTime;
	}

	public void setLastThreadTime(Date lastThreadTime) {
		this.lastThreadTime = lastThreadTime;
	}

	public Short getEnabled() {
		return this.enabled;
	}

	public void setEnabled(Short enabled) {
		this.enabled = enabled;
	}

	public short getTop() {
		return this.top;
	}

	public void setTop(Short top) {
		this.top = top;
	}

	public Short getCommend() {
		return commend;
	}

	public void setCommend(Short commend) {
		this.commend = commend;
	}

	public Short getElite() {
		return this.elite;
	}

	public void setElite(Short elite) {
		this.elite = elite;
	}

	public Short getSource() {
		return this.source;
	}

	public void setSource(Short source) {
		this.source = source;
	}

	public Short getOriginal() {
		return this.original;
	}

	public void setOriginal(Short original) {
		this.original = original;
	}

	public Short getReply() {
		return this.reply;
	}

	public void setReply(Short reply) {
		this.reply = reply;
	}

	public Short getNotice() {
		return this.notice;
	}

	public void setNotice(Short notice) {
		this.notice = notice;
	}

	public Long getBoardid() {
		return boardid;
	}

	public void setBoardid(Long boardid) {
		this.boardid = boardid;
	}

	public String getContentAbstract() {
		return contentAbstract;
	}

	public void setContentAbstract(String contentAbstract) {
		this.contentAbstract = contentAbstract;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileExt() {
		return fileExt;
	}

	public void setFileExt(String fileExt) {
		this.fileExt = fileExt;
	}

	public String getFileFlag() {
		return fileFlag;
	}

	public void setFileFlag(String fileFlag) {
		this.fileFlag = fileFlag;
	}

	public String getHtmlTitle(){
		return StringUtil.getChineseSubString(title, 30, "…");
	}
	public String getTitle15(){
		return StringUtil.getChineseSubString(title, 15, "");
	}
	/**
	 * 作为推荐显示
	 */
	public String getHtmlCommendTitle(){
		return StringUtil.getChineseSubString(title, 20, "…");
	}
	public String getWmlTitle(){
		return StringUtil.getChineseSubString(title, 8, "…");
	}
	public String getWmlPath(){
		return getAbsolutePath(Constants.USER_AGENT_TYPE_WML);
	}
	
	public String getHtmlPath(){
		return getAbsolutePath(Constants.USER_AGENT_TYPE_HTML);
	}
	
	private String getAbsolutePath(String uat){
		String errorPage = "topic_notexist.jsp";
		if(this.enabled.compareTo(Short.valueOf("1")) != 0){
			return errorPage;
		}
		String path = Constants.FILE_SEP + BbsUtil.getPrefixPath(uat) + filePath + fileName + fileExt;
		if(!existFile(uat)){ // 文件不存在
			try {
				BbsUtil.writeForumFile(content, this.getBoardid(), this.getCreateTime(), uat);
				updateFileFlag(uat);
			} catch (IOException e) {
				return errorPage;
			}
		}
		return path;
	}
	private void updateFileFlag(String uat){
		try{
			if(fileFlag == null || "".equals(fileFlag))
				fileFlag = "0";
			if(uat.equals(Constants.USER_AGENT_TYPE_WML)){
				fileFlag = fileFlag.substring(0, fileFlag.length() - 1) + "1";
			}
			if(uat.equals(Constants.USER_AGENT_TYPE_HTML)){
				if(fileFlag.length() < 2)
					fileFlag = "0" + fileFlag;
				fileFlag = fileFlag.substring(0, fileFlag.length() -2) + "1" + fileFlag.substring(fileFlag.length()-1);
			}
			TopicManager manager = (TopicManager) ApplicationManager.appContext.getBean("topicManager");
			manager.saveObject(this);
		}catch(Exception e){
		}
	}
	private boolean existFile(String uat){
		if(fileFlag == null || "".equals(fileFlag))
			return false;
		if(uat.equals(Constants.USER_AGENT_TYPE_WML)){
			String value = fileFlag.substring(fileFlag.length() -1, fileFlag.length());
			if(value.equals("1")){
				return true;
			}
			return false;
		}
		if(uat.equals(Constants.USER_AGENT_TYPE_HTML)){
			if(fileFlag.length() < 2)
				return false;
			String value = fileFlag.substring(fileFlag.length() -2, fileFlag.length()-1);
			if(value.equals("1")){
				return true;
			}
			return false;
		}
		return false;
	}

	@Override
	public boolean equals(Object o) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return null;
	}

}
